package com.tangding.service.dao.role;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.tangding.common.exception.GException;
import com.tangding.common.logx.LogHelp;
import com.tangding.common.utils.JacksonUtil;
import com.tangding.service.db.BaseMongoRepository;
import com.tangding.service.db.MongoDBOption;
import com.tangding.service.entity.role.Permission;

/**
 * @description 
 *
 * @author Ranger
 * @version 2017年11月9日_上午10:05:37
 *
 */
@Repository
public class PermissionRepository extends BaseMongoRepository<Permission, Serializable> {

	Map<Integer, Permission> permissionMap = new HashMap<>();
	
	@Override
	protected void afterInitClient() {
		option = new MongoDBOption("ACCOUNT", "permission");
		
		try {
			List<Permission> permissionList = findAll();
			if (permissionList != null) {
				permissionList.forEach(permission -> {
					permissionMap.put(permission.getId(), permission);
				});
			}
		} catch (GException e) {
			LogHelp.doLogAppErr(e);
			System.exit(-1);
		}
	}
	
	public Map<Integer, Permission> getPermissions() {
		return permissionMap;
	}
	
	public Permission getPermission(Integer permissionId) {
		return permissionMap.get(permissionId);
	}
	
	public static void main(String[] args) {
		List<Permission> ps = new ArrayList<>();
		
		Permission permission = new Permission();
		permission.setName("森林舞会");
		permission.setId(1);
		permission.setKey("slwh");
		ps.add(permission);

		permission = new Permission();
		permission.setName("奔驰宝马");
		permission.setId(2);
		permission.setKey("bcbm");
		ps.add(permission);

		permission = new Permission();
		permission.setName("金鲨银鲨");
		permission.setId(3);
		permission.setKey("jsys");
		ps.add(permission);

		permission = new Permission();
		permission.setName("金蟾捕鱼");
		permission.setId(4);
		permission.setKey("jcby");
		ps.add(permission);

		permission = new Permission();
		permission.setName("帐号管理");
		permission.setId(5);
		permission.setKey("zhgl");
		ps.add(permission);

		permission = new Permission();
		permission.setName("神龙宝藏");
		permission.setId(6);
		permission.setKey("slbz");
		ps.add(permission);

		System.out.println(JacksonUtil.toJson(ps));
	}
}
